import 'dart:async';
import 'dart:developer';
import 'dart:io';
import 'package:huxiaodui/api/api.dart';
import 'package:aliyun_push/aliyun_push.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:huxiaodui/config.dart';
import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';

Future<bool> isHarmonyOS() async {
  final deviceInfo = DeviceInfoPlugin();

  if (Platform.isAndroid) {
    final androidInfo = await deviceInfo.androidInfo;
    final manufacturer = androidInfo.manufacturer.toLowerCase() ?? '';
    final brand = androidInfo.brand.toLowerCase() ?? '';
    final display = androidInfo.display.toLowerCase() ?? '';

    // 方式1：判断品牌或制造商是否包含 "huawei" 或 "honor"
    if (manufacturer.contains('huawei') || brand.contains('huawei') || brand.contains('honor')) {
      // 方式2：进一步判断系统显示版本中是否含 Harmony
      if (display.contains('harmony') || display.contains('hongmeng')) {
        return true;
      }
    }
  }

  return false;
}


class SystemPush {
  static final _aliyunPush = AliyunPush();

  static var deviceId = "";


  static Future<void> init() async {

    if(!Platform.isIOS && !(await isHarmonyOS()))return;

    _aliyunPush.addMessageReceiver(
        onNotification: _onNotification,
        onNotificationOpened: _onNotificationOpened,
        onNotificationRemoved: _onNotificationRemoved,
        onMessage: _onMessage,
        onAndroidNotificationReceivedInApp: _onAndroidNotificationReceivedInApp,
        onAndroidNotificationClickedWithNoAction:
            _onAndroidNotificationClickedWithNoAction,
        onIOSChannelOpened: _onIOSChannelOpened,
        onIOSRegisterDeviceTokenSuccess: _onIOSRegisterDeviceTokenSuccess,
        onIOSRegisterDeviceTokenFailed: _onIOSRegisterDeviceTokenFailed);

    _aliyunPush
        .initPush(appKey: AliYunConfig.appKeyIos, appSecret: AliYunConfig.appSecretIos)
        .then((initResult) {
      var code = initResult['code'];
      if (code == kAliyunPushSuccessCode) {
        log("初始化推送成功 $deviceId");
      } else {
        String errorMsg = initResult['errorMsg'];
        log('初始化推送失败, errorMsg: $errorMsg');
      }
    });

    if (Platform.isAndroid) {
      _aliyunPush.initAndroidThirdPush().then((initResult) {
        var code = initResult['code'];
        if (code == kAliyunPushSuccessCode) {
          log("初始化辅助通道成功");
        } else {
          String errorMsg = initResult['errorMsg'];
          log('初始化辅助通道成功, errorMsg: $errorMsg');
        }
      });
    }

    await _aliyunPush.showIOSNoticeWhenForeground(true);
    Timer.periodic(const Duration(seconds: 3), (timer) async {
      deviceId = await _aliyunPush.getDeviceId();
      if(deviceId.isEmpty) {
        await init();
      }else{
        Api.mobileApp.updateDeviceId();
        timer.cancel();
      }
    });

  }

  static Future<void> _onNotification(Map<dynamic, dynamic> message) async {
    log('onNotification: $message');
  }

  static Future<void> _onAndroidNotificationReceivedInApp(
      Map<dynamic, dynamic> message) async {
    log('onAndroidNotificationReceivedInApp: $message');
  }

  static Future<void> _onMessage(Map<dynamic, dynamic> message) async {
    log('onMessage: $message');
  }

  static Future<void> _onNotificationOpened(
      Map<dynamic, dynamic> message) async {
    log('onNotificationOpened: $message');
  }

  static Future<void> _onNotificationRemoved(
      Map<dynamic, dynamic> message) async {
    log('onNotificationRemoved: $message');
  }

  static Future<void> _onAndroidNotificationClickedWithNoAction(
      Map<dynamic, dynamic> message) async {
    log('onAndroidNotificationClickedWithNoAction: $message');
  }

  static Future<void> _onIOSChannelOpened(
      Map<dynamic, dynamic> message) async {}

  static Future<void> _onIOSRegisterDeviceTokenSuccess(
      Map<dynamic, dynamic> message) async {
    log('APNs注册成功, $message');
  }

  static Future<void> _onIOSRegisterDeviceTokenFailed(
      Map<dynamic, dynamic> message) async {
    EasyLoading.showError('注册APNs失败, errorMsg: $message');
  }

  static bindAccount(String account){
    AliyunPush().bindAccount(account);
  }

  static unbindAccount(){
    AliyunPush().unbindAccount();
  }
}
